/**
 * Created by AllanXu on 7/25/2016.
 */
define(function (require) {
	var Vue = require('vue'),
        Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities'),
        userId = sessionStorage.id;

    var id = utilities.getParameterByName('id');
    // 认证
    var AuthDialog = require('auth_dialog');
    var authDialog = new AuthDialog();
    // 禁用申诉
    var Forbidden = require('forbidden');
    var forbidden = new Forbidden();
	var app = new Vue({
		el: '#tieba_detail',
		data: {
			pageCount: '',
			pageno: 1,
            postInfo: {},
            pageAnswer: {
                listArr: []
            },
            isReplyShow: false,
            userMsg: '',
            bottomTxt: '',
			picServer: Config.picServer
		},
		methods:{
			init: function () {
				_(Config.SERVICE.TIEBA.DETAILS, {
                    "id": id,
                    "pageno": app.pageno,
                    "pagesize": 10
                },function(data) {
                    app.postInfo = data.obj.post;
                    if(app.pageno == 1){
                        app.pageAnswer.listArr = data.obj.pageAnswer.pageitems;
                        app.pageCount = data.obj.pageAnswer.pagecount;
                    }else{
                        app.pageAnswer.listArr = app.pageAnswer.listArr.concat(data.obj.pageAnswer.pageitems);
                    }
                    console.log(12,app.postInfo);
                });
			},
            goDetail: function (id) {
                location.href = 'tieba.html?id='+id;
            },
            goReply: function(){
                // 是否认证/禁用
                // base64TojsonObj
                var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if(userInfo.member.sfrz==0){    // 认证
                    authDialog.show();
                }else if(userInfo.member.sfjy==1){  // 禁用
                    forbidden.show();
                }else{
                    console.log("已认证、没有禁用");
                    _(Config.SERVICE.MATES.VERIFY+'?id=' + userId, {}, function(data) {
                        console.log(data,2444444);
                        if(data.errmsg.indexOf("择偶") > 0 ){
                            // 择偶条件没有完善跳转到择偶条件
                             if(confirm("为更好地为您提供服务，请在“个人编辑”中完善个人资料或择偶条件。")){
                                location.href = 'uc_mate_condition.html';
                             }
                        }else if(data.errmsg.indexOf("基本信息") > 0){
                            if(confirm("为更好地为您提供服务，请在“个人编辑”中完善个人资料或择偶条件。")){
                                location.href = 'uc_profile.html';
                             }
                        }else{
                            app.isReplyShow = !app.isReplyShow;
                        }
                    });
                }
            },
            reply: function(){
                _(Config.SERVICE.TIEBA.REPLY, {
                    "themeAnswer.post.id": id,
                    "themeAnswer.member.id": userId,
                    "themeAnswer.content": app.userMsg
                },function(data) {
                    app.isReplyShow = !app.isReplyShow;
                    app.userMsg='';
                    if(data.code==200)
                        alert("回复成功!");
                    
                    app.init();
                });
            }
		}
	});
	app.init();

    window.onscroll = function(){
        var scrolltop = document.body.scrollTop | document.documentElement.scrollTop;
        var clientHeight = document.documentElement.clientHeight;
        console.log(scrolltop + clientHeight, document.body.clientHeight)
        if(scrolltop + clientHeight  == document.body.clientHeight){
            if (app.pageno <= app.pageCount){
                app.pageno++;
                app.init();
            }else{
                app.bottomTxt = '到底儿了~';
            }
        }
    }
});